﻿Imports System.Windows.Forms
Imports System.IO
Imports System.Text
Public Class DUCDialog
    Dim _gamecode As String = "####"
    Dim titlefilename As String = ""
    Private _filename As String = ""
    Public Overloads Function ShowDialog(ByVal Filename As String, ByVal SaveDescription As String, ByVal EnableCustomDesc As Boolean) As DialogResult
        _filename = Filename
        txtSaveDesc.Text = SaveDescription
        txtSaveDesc.ReadOnly = Not EnableCustomDesc
        Return MyBase.ShowDialog
    End Function
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'If Not IO.File.Exists(titlefilename) AndAlso Not String.IsNullOrEmpty(titlefilename) Then
        '    Dim s As New Settings
        '    If s.Setting("SendKeys") = "1" Then
        '        If MessageBox.Show("The developer of this tool is in the process of collecting game titles." & vbCrLf & "Do you want to help him by sending him your game title?", "Save Type Converter", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        '            Dim c As New ServiceReference1.offlinescSoapClient
        '            MessageBox.Show(c.SubmitGID(_gamecode, txtGameName.Text))
        '        End If
        '    End If
        'End If
        If Not String.IsNullOrEmpty(titlefilename) Then
            Dim d As String = IO.Path.GetDirectoryName(titlefilename)
            If Not IO.Directory.Exists(d) Then
                IO.Directory.CreateDirectory(d)
            End If
            IO.File.WriteAllText(titlefilename, txtGameName.Text)
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    Public ReadOnly Property GameName() As String
        Get
            Return txtGameName.Text
        End Get
    End Property
    Public ReadOnly Property SaveName() As String
        Get
            Return txtSaveName.Text
        End Get
    End Property
    Public ReadOnly Property SaveDescription()
        Get
            Return txtSaveDesc.Text
        End Get
    End Property
    ''' <summary>
    ''' Returns the game code of an NDS ROM at the specified location.  Ex. US Super Mario 64 DS returns ASME
    ''' </summary>
    ''' <param name="Filename">Location of the NDS ROM to get the game code from</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetGameCode(ByVal Filename As String) As String
        Dim input As New FileStream(Filename, FileMode.Open)
        Dim reader As New BinaryReader(input)
        reader.BaseStream.Position = 12
        Dim code As String = Encoding.UTF8.GetString(reader.ReadBytes(4))
        input.Close()
        Return code
    End Function
    Private Sub DUCDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim code As String = ""
        Dim sav As New IO.FileInfo(_filename)
        Dim ndsfile As String = sav.Directory.FullName & "/" & sav.Name.Replace("." & SaveExtension(sav.Name), "") & ".nds"
        If IO.File.Exists(ndsfile) Then
            code = GetGameCode(ndsfile)
        End If
        If code = "" Then
            _filename = sav.Directory.FullName & "/" & sav.Name & ".nds"
            If IO.File.Exists(ndsfile) Then
                code = GetGameCode(ndsfile)
            End If
        End If
        If False Then
            titlefilename = Environment.CurrentDirectory & "/gamedb/" & code(3) & "/" & code.Substring(0, 3) & ".txt"
        Else
            titlefilename = Environment.CurrentDirectory & "/gamedb/" & code & ".txt"
        End If
        If Not code = "" AndAlso code.Length = 4 Then
            If IO.File.Exists(titlefilename) Then
                txtGameName.Text = IO.File.ReadAllText(titlefilename)
            End If
            _gamecode = code
            'If IO.File.Exists(Form1.CurrentDirectory & "/ndsnamedb.txt") Then
            '    Dim m As New Converter.NDSROMManager(Form1.CurrentDirectory & "/ndsnamedb.txt")
            '    txtGameName.Text = m.GameName(code)
            'End If
        End If
    End Sub
    Private Function SaveExtension(ByVal Filename As String) As String
        Dim parts() As String = Filename.Split(".")
        If parts.Length = 2 Then
            Return parts(1)
        ElseIf parts.Length < 2 Then
            Return ""
        ElseIf parts(parts.Length - 2).Length = 1 Then
            If IsNumeric(parts(parts.Length - 2)) Then
                Return parts(parts.Length - 2) & "." & parts(parts.Length - 1)
            Else
                Return parts(parts.Length - 1)
            End If
        Else
            Return parts(parts.Length - 1)
        End If
    End Function
End Class
